// 商品分类专区楼层页面js

window.onload = function() {
    //获取标签
    var box = document.getElementById('box');
    var allBoxs = box.children;
    var aside = document.getElementById('aside');
    var lis = aside.children;
    var isClick = false; //默认没有被点击



    for (var j = 0; j < lis.length; j++) {
        lis[j].index = j; //给每一个li标签的索引赋值为j
        //为每一个li标签绑定点击事件
        lis[j].onclick = function() {
            isClick = true; //点击
            //点击后，清除每一个li标签原有的类名
            for (var k = 0; k < lis.length; k++) {
                lis[k].className = '';
            }
            //点击后，给每一个li标签附上新类名
            this.className = 'current on';
            //页面动画起来
            startAnimation(document.documentElement, {
                    "scrollTop": this.index * document.body.clientHeight
                }, function() {
                    isClick = false;
                })
                //document.documentElement.scrollTop=this.index * document.body.clientHeight
        }
    }

    //监听页面滚动
    window.onscroll = function() {
        if (!isClick) {
            //获取页面滚动的高度
            var docScrollTop = document.documentElement.scrollTop || document.body.scrollTop;
            for (var i = 0; i < lis.length; i++) {
                if (docScrollTop > allBoxs[i].offsetTop) {
                    for (var j = 0; j < allBoxs.length; j++) {
                        lis[j].className = '';
                    }
                    lis[i].className = 'current on';
                }
            }
        }
    }

    //设置秒杀结束的时间
    var endtime = new Date('2021-08-14 00:00:00');
    var endseconds = endtime.getTime(); //60*1000

    //声明变量保存剩余的时间
    var d = h = m = s = 0;
    //设置定时器，实现限时秒杀效果
    var id = setInterval(seckill, 1000);

    function seckill() {
        var nowtime = new Date(); //获取当前时间
        //获取时间差，单位秒
        var remaining = parseInt((endseconds - nowtime.getTime()) / 1000); //从秒杀开始到秒杀结束持续的时间
        //判断秒杀是否过期
        if (remaining > 0) {
            d = parseInt(remaining / 86400);
            h = parseInt((remaining / 3600) % 24);
            m = parseInt((remaining / 60) % 60);
            s = parseInt(remaining % 60);

            d = d < 10 ? '0' + d : d;
            h = h < 10 ? '0' + h : h;
            m = m < 10 ? '0' + m : m;
            s = s < 10 ? '0' + s : s;
        } else {
            clearInterval(id);
            d = h = m = s = '00';
        }


        document.getElementById('d').innerHTML = d + '天';
        document.getElementById('h').innerHTML = h + '时';
        document.getElementById('m').innerHTML = m + '分';
        document.getElementById('s').innerHTML = s + '秒';

    }
}